//
//  QHScrollPageView.m
//  Bilibili~
//
//  Created by lanou on 16/5/10.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "QHScrollPageView.h"
#import "QHScrollSegmentView.h"
#import "UIView+QHExtension.h"
@interface QHScrollPageView ()<UIScrollViewDelegate> {
    CGFloat oldOffSetX; // 之前的偏移量
    NSInteger oldIndex;
    NSInteger currentIndex;
    CGFloat Margin; // margin为scrollSegment的两边留的空间多大
}
@property (nonatomic, strong) QHScrollSegmentView *SegmentView;
@property (nonatomic, weak) UIViewController *parentViewController;
@property (nonatomic, strong) NSArray *childVcs;
@property (nonatomic, strong) NSArray *titlesArray;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@end
@implementation QHScrollPageView
- (UIScrollView *)mainScrollView {
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 44)];
        _mainScrollView.contentSize = CGSizeMake(ScreenWidth * self.childVcs.count, 0);
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.bounces = NO; // 糖糖糖
        _mainScrollView.pagingEnabled = YES;
        _mainScrollView.delegate = self;
        [self addSubview:_mainScrollView];
    }
    return _mainScrollView;
}
- (QHScrollSegmentView *)SegmentView {
    if (!_SegmentView) {
        _SegmentView = [[QHScrollSegmentView alloc] initWithFrame:CGRectMake(Margin, 0, ScreenWidth - Margin * 2, NavigationBarHeight + 20) titles:self.titlesArray index:currentIndex];
        [self addSubview:_SegmentView];
    }
    return _SegmentView;
}
- (instancetype)initWithFrame:(CGRect)frame childVcs:(NSArray *)childVcs parentViewController:(UIViewController *)parentViewController margin:(CGFloat)margin index:(NSInteger)index{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.childVcs = childVcs;
        oldIndex = index;
        currentIndex = index; // 先赋值 再更新UI
        oldOffSetX = 0.0;
        Margin = margin;
        [self commonInit];
        [self addSubChildVcsByVc];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:indexChangedNotification object:nil]; // 接收到下标改变得通知
    }
    return self;
}
- (void)commonInit {
    NSMutableArray *tempTitles = [NSMutableArray array];
    for (UIViewController *childVc in self.childVcs) {
        NSAssert(childVc.title, @"子控制器的title没有正确设置!!");
        if (childVc.title) {
            [tempTitles addObject:childVc.title];
        }
    }
    self.titlesArray = [NSArray arrayWithArray:tempTitles];
    // 触发懒加载
    self.SegmentView.backgroundColor = [UIColor clearColor];
}
- (void)addSubChildVcsByVc {
    NSInteger index = 0;
    for (UIViewController *childV in self.childVcs) {
        childV.view.frame = CGRectMake(index * ScreenWidth, 0, ScreenWidth, ScreenHeight - 64 - 44);
        [self.mainScrollView addSubview:childV.view];
        index++;
    }
    // 根据初始的下标给相应的界面
    self.mainScrollView.contentOffset = CGPointMake(currentIndex * ScreenWidth, 0);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offSetX = scrollView.contentOffset.x;
    CGFloat temp = offSetX / ScreenWidth;
    CGFloat progress = temp - floor(temp);
    if (offSetX - oldOffSetX >= 0) {
        if (progress == 0.0) {
            return;
        }
        oldIndex = (offSetX / ScreenWidth);
        currentIndex = oldIndex + 1;
        if (currentIndex >= self.childVcs.count) {
            currentIndex = self.childVcs.count - 1;
            return;
        }
    } else {
        currentIndex = (offSetX / ScreenWidth);
        oldIndex = currentIndex + 1;
        if (oldIndex >= self.childVcs.count) {
            oldIndex = self.childVcs.count - 1;
            return;
        }
        progress = 1.0 - progress;
    }
    if(self.SegmentView) {
    [self.SegmentView updateUIWithProgress:progress oldIndex:oldIndex currentIndex:currentIndex];
    }
}
- (void)updateView:(NSNotification *)notification {
    currentIndex = _SegmentView.currentIndex;
    self.mainScrollView.contentOffset = CGPointMake(ScreenWidth * _SegmentView.currentIndex, 0);
}
/**为了解决在滚动或接着点击title更换的时候因为index不同步而增加了下边的两个代理方法的判断*/
/** 滚动减速完成时再更新title的位置 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger nowIndex = (scrollView.contentOffset.x / ScreenWidth);
    if(self.SegmentView) {
        [self.SegmentView updateUIWithProgress:1.0 oldIndex:oldIndex currentIndex:nowIndex];
    }
}
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//    oldOffSetX = scrollView.contentOffset.x;
//}


- (void)dealloc {
    // 移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
